package com.kc.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.bus.domain.Goods;
import com.kc.bus.domain.Inport;
import com.kc.bus.domain.Provider;
import com.kc.bus.service.GoodsService;
import com.kc.bus.service.InportService;
import com.kc.bus.service.ProviderService;
import com.kc.bus.vo.GoodsVo;
import com.kc.bus.vo.InportVo;
import com.kc.sys.common.*;
import com.kc.sys.domain.User;
import com.kc.sys.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-04
 */
@RestController
@RequestMapping("/inport")
public class InportController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private InportService inportService;
    @RequestMapping("loadAllInport")
    public DataGridView loadAllInport(InportVo inportVo){

        IPage<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(inportVo.getProviderid()!=null&&inportVo.getProviderid()!=0,"providerid",inportVo.getProviderid());
        queryWrapper.eq(inportVo.getGoodsid()!=null&&inportVo.getGoodsid()!=0,"goodsid",inportVo.getGoodsid());
        //大于ge
        queryWrapper.ge(inportVo.getStartTime()!=null, "inporttime", inportVo.getStartTime());
        //小于
        queryWrapper.le(inportVo.getEndTime()!=null, "inporttime", inportVo.getEndTime());
        queryWrapper.orderByDesc("inporttime");
        inportService.page(page,queryWrapper);
        List<Inport> records = page.getRecords();
        for (Inport inport : records) {
            Provider provider = providerService.getById(inport.getProviderid());
            if(null!=provider){
                inport.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(inport.getGoodsid());
            if (null!=goods){
                inport.setGoodsname(goods.getGoodsname());
                inport.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //添加商品进货
    @RequestMapping("addInport")
    public R addInport(InportVo inportVo){
        try {
            inportVo.setInporttime(new Date());
            User user = (User) WebUtils.getSession().getAttribute("user");
            inportVo.setOperateperson(user.getName());
            inportService.save(inportVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新商品进货
    @RequestMapping("updateInport")
    public R updateInport(InportVo inportVo){
        try {
            inportService.updateById(inportVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteInport")
    public R deleteById(Integer id){
        try {
            inportService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    /**
     * 批量删除
     * @param --logInfoVo
     * @return
     */
    @RequestMapping("deleteInports")
    public R deleteByIds(InportVo inportVo){
        try {
            ArrayList<Serializable> ids = new ArrayList<>();
            for (Integer id : inportVo.getIds()) {
                ids.add(id);
            }
          /*  ArrayList<String> inportimgs = new ArrayList<>();
            for (String inportimg : inportimgs) {
                if (!inportimg.equals(Constast.IMAGES_DEFAULTGOODSIMG_PNG)){
                    AppFileUtils.removeFileByPath(inportimg);
                }
            }*/
            inportService.removeByIds(ids);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }
}

